package com.receiver;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class PositionService extends Service {

	private Timer updateTimer;
	private int interval = 5;
	private Context ctx;
	private int invokedCount = 0;
	
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		updateTimer = new Timer("updatePosition");
		updateTimer.scheduleAtFixedRate(sendToServer, 0, interval * 1000);
		
		return Service.START_STICKY;
	}
	
	private TimerTask sendToServer = new TimerTask() {
		public void run() {
			invokedCount++;
			SendUDPMessage(); // Send current GPS data package to server by using UDP socket
			//Toast.makeText(ctx, "Service Stopped ...", Toast.LENGTH_SHORT).show();
			
		}
		
		/*
		 * IP/port: 112.213.84.103/8800
		   <ID>;$GPRMC,224727.000,A,2109.0727,N,10530.4011,E,0.00,64,120611,,*3D,,*3D
           ID: 84123456789
		 */
		double nat = 2109.0727, lgn = 10530.4011;
		private String GetNMEAMessage()
		{
			Random randomGenerator = new Random();
			
			int rNat = randomGenerator.nextInt(100), rLgn = randomGenerator.nextInt(100);
			boolean needMinus = randomGenerator.nextBoolean();
			if (needMinus)
			{
				nat = nat - (double) rNat / 1000; 
				lgn = lgn - (double) rLgn / 1000;
			} else 
			{
				nat = nat + (double) rNat / 1000; 
				lgn = lgn + (double) rLgn / 1000;
			}
			
			NumberFormat formatter = new DecimalFormat("#.####");
			SimpleDateFormat dateFormat = new SimpleDateFormat("HHmmss");
	        Date date = new Date();
	        String currentTime = dateFormat.format(date);
			String messageStr="84123456789;$GPRMC," + currentTime + ".000,A," + formatter.format(nat) + ",N," + formatter.format(lgn) + ",E,0.00,64,031211,,*3D,,*3D";
			
			return messageStr;
		}
		
		private void SendUDPMessage() {
			int server_port = 8800;
			DatagramSocket s = null;
			try {
				s = new DatagramSocket();
			} catch (SocketException e) {
				e.printStackTrace();
			}
			InetAddress local = null;
			try {
				local = InetAddress.getByName("112.213.84.103");
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
			
			String messageStr = GetNMEAMessage();
			int msg_length=messageStr.length();
			byte[] message = messageStr.getBytes();
			DatagramPacket p = new DatagramPacket(message, msg_length, local,server_port);
			try {
				s.send(p);
				
				int i = 0;
				i = i + 1;
			} catch (IOException e) {
				e.printStackTrace();
			}	
		}
	};
	
	public void onCreate() {
		updateTimer = new Timer("updatePosition");
		ctx = this;
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
}
